<template>
  <div class="deploy-detail">
    <span class="table-blue" @click="back">
      <i class="iconfont icon-left"></i>返回
    </span>
    <Form  class="deploySetting" ref="deploySetting" :rules="ruleValidate" :model="deploySetting" label-position="right" :label-width="80">
      <Row>
        <i-col span="18">
          <Row>
            <i-col span="12">
              <FormItem label="英文名称" prop="name">
                <Row>
                  <i-col span="24">
                    <i-input v-model="deploySetting.name" style="width:300px"></i-input>
                  </i-col>
                </Row>
              </FormItem>
            </i-col>
          </Row>
        </i-col>
      </Row>
      <Row>
        <i-col span="18">
          <Row>
            <i-col span="12">
              <FormItem label="启动环境" prop="cnName">
                <Row>
                  <i-col span="24">
                    <Select v-model="deploySetting.defaultSpringProfile" filterable style="width:300px">
                      <Option v-for="item in environment" :value="item.value" :key="item.key">{{ item.value }}</Option>
                    </Select>
                  </i-col>
                </Row>
              </FormItem>
            </i-col>
          </Row>
        </i-col>
      </Row>
      <Row>
        <i-col span="18">
          <Row>
            <i-col span="12">
              <FormItem label="地址" prop="gitUrl">
                <Row>
                  <i-col span="24">
                    <i-input v-model="deploySetting.gitUrl" style="width:300px"></i-input>
                  </i-col>
                </Row>
              </FormItem>
            </i-col>
          </Row>
        </i-col>
      </Row>
      <Row>
        <i-col span="24">
          <Row>
            <i-col span="24">
              <FormItem label="应用选择" v-for="(item, index) in appList" :key="index">
                <Row>
                  <i-col span="7">
                    <i-select
                      filterable
                      placeholder="请选择"
                      style="width:300px"
                    >
                      <i-option
                        v-for="(items,indexs) in appData"
                        :key="indexs"
                        :value="items.id"
                        @click.native="chooseApp(items,index)"
                      >{{items.name}}</i-option>
                    </i-select>
                  </i-col>
                  <i-col span="7">
                    <i-select
                      filterable
                      placeholder="请选择"
                      style="width:300px"
                    >
                      <i-option
                        v-for="(items,indexs) in versionList[index]"
                        :key="indexs"
                        :value="items.id"
                        @click.native="chooseVersion(items,index)"
                      >{{items.version}}</i-option>
                    </i-select>
                  </i-col>
                  <i-col span="3">
                    <Button @click="handleRemove(index)">删除</Button>
                  </i-col>
                </Row>
              </FormItem>
              <FormItem>
                <Row>
                  <i-col span="4">
                    <Button type="dashed" long @click="handleAdd()" icon="md-add">增加</Button>
                  </i-col>
                </Row>
              </FormItem>
            </i-col>
          </Row>
        </i-col>
      </Row>
      <Row>
        <i-col span="18">
          <Row>
            <i-col span="12">
              <FormItem label="自动部署">
                <Row>
                  <i-col span="24">
                    <RadioGroup v-model="chooseDeploy" style="width:200px" @on-change="chooseAutoDeploy">
                      <Radio label='yes'>
                        <span>是</span>
                      </Radio>
                      <Radio label='no'>
                        <span>否</span>
                      </Radio>
                    </RadioGroup>
                  </i-col>
                </Row>
              </FormItem>
            </i-col>
          </Row>
        </i-col>
      </Row>
      <Row>
        <i-col span="24">
          <Row>
            <i-col span="24">
              <FormItem>
                <Button type="primary" @click="ascertain('deploySetting')">提交</Button>
                <Button style="margin-left: 8px" @click="cancel()">关闭</Button>
              </FormItem>
            </i-col>
          </Row>
        </i-col>
      </Row>
    </Form>
  </div>
</template>

<script>
import { getList, getAppVersion } from 'http/api/application';
export default {
  name: 'DeployDetail',
  data () {
    return {
      chooseDeploy: '',
      deploySetting: {
        name: '',
        defaultSpringProfile: '',
        gitUrl: '',
        autoDeploy: true,
        deployAppList: [{
          appId: '',
          appName: '',
          version: '',
          branch: '',
          gray: ''
        }]
      },
      appList: [],
      versionList: {},
      environment: [
        {
          key: '0',
          value: 'dev'
        },
        {
          key: '1',
          value: 'test'
        },
        {
          key: '2',
          value: 'pre'
        },
        {
          key: '3',
          value: 'prod'
        }
      ],
      ruleValidate: {
        name: [
          { required: true, message: '中文名称不能为空', trigger: 'blur' }
        ],
        cnName: [
          { required: true, message: '英文名称不能为空', trigger: 'blur' }
        ],
        gitUrl: [
          { required: true, message: '地址不能为空', trigger: 'change' }
        ]
      },
      appData: []
    }
  },
  methods: {
    back () {
      this.$router.push({ path: '/home/deploy/deployList' })
    },
    handleRemove (index) {
      this.appList.splice(index, 1);
      this.deploySetting.deployAppList.splice(index, 1);
      this.versionList.splice(index, 1);
    },
    handleAdd () {
      this.appList.push({});
      this.deploySetting.deployAppList.push({
        appId: '',
        appName: '',
        version: '',
        branch: '',
        gray: ''
      })
    },
    chooseVersion (value, index) {
      this.deploySetting.deployAppList[index].version = value.version;
      this.deploySetting.deployAppList[index].branch = value.branch;
      console.log(this.deploySetting.deployAppList)
    },
    chooseApp (value, index) {
      this.deploySetting.deployAppList[index].appId = value.id;
      this.deploySetting.deployAppList[index].appName = value.name;
      this.getAppVersion(value.id, index);
    },
    chooseAutoDeploy (value) {
      console.log(value)
      if (value === 'yes') {
        this.deploySetting.autoDeploy = true;
      } else {
        this.deploySetting.autoDeploy = false;
      }
    },
    getAppVersion (id, index) {
      let param = {
        id: id,
        page: 1,
        size: 100000
      };
      getAppVersion(param).then(res => {
        if (res) {
          this.$set(this.versionList, index, res.items);
          console.log(this.versionList[index])
        }
      })
        .catch(err => {
          this.$Message.error(err);
        })
    },
    getList () {
      let param = {
        page: 1,
        size: 100000
      };
      getList(param).then(res => {
        if (res) {
          this.appData = res.items;
          this.pagination.total = res.total;
        }
      })
        .catch(err => {
          this.$Message.error(err);
        })
    }
  },
  created () {

  },
  mounted () {
    this.getList();
    this.appList.push({})
  }
}
</script>

<style rel="stylesheet/less" lang="less" scoped>
.deploySetting {
    padding:20px;
  border-top: 1px solid #d6d6d6;
  .title {
    padding-left: 140px;
    font-size: 16px;
    line-height: 40px;
    font-weight: bold;
    padding-top: 20px;
  }
  .box {
    border-bottom: 1px dashed #d6d6d6;
  }
}
.deploy-detail {

  .table-blue {
    display: inline-block;
    margin: 20px;
    font-size: 12px;
    .icon-left {
      font-size: 12px;
    }
  }
}
</style>

<style lang="less">
.visual-outer-model {
  .ivu-modal-body {
    padding-bottom: 72px;
  }
  .visual-outer-footer {
    float: right;
    margin-top: 20px;
  }
  .ivu-checkbox-wrapper {
    margin-bottom: 10px;
    margin-right: 10px;
  }
  .outer-name {
    .ivu-input-wrapper {
      width: 280px;
    }
  }
}
</style>
